package v1

import (
	"RoccBlog/api"
	"RoccBlog/global"
	"RoccBlog/initalize"
	"RoccBlog/model"
	v1 "RoccBlog/service/v1"
	"github.com/gin-gonic/gin"
	"net/http"
	"strconv"
)

type CacheController struct {
	api.BaseController
	Service *v1.CacheService
}

func init() {
	global.RegisterController(&CacheController{})
}

func (c *CacheController) RegisterController(engine *gin.RouterGroup) {
	c.Service = global.GetService(v1.CacheService{}).(*v1.CacheService)

	g := engine.Group("/cache")
	{
		g.POST("", c.Add)
		g.DELETE("", c.Remove)
		g.PUT("", c.Modify)
		g.GET("/list", c.Find)
		g.GET("", c.FindById)
		g.POST("/value", c.AddCacheValue)
		g.DELETE("/value", c.RemoveCacheValue)
		g.GET("/refresh", c.RefreshGlobalCache)
		g.GET("/globalCache", c.GetGlobalCacheValue)
	}
}

// Add
// @Tags 缓存管理
// @Summary 添加缓存
// @Produce  json
// @Param cache body model.Cache required "缓存"
// @Success 200 {object} api.Result "成功"
// @Router /cache [post]
func (c *CacheController) Add(ctx *gin.Context) {
	var cache model.Cache
	err := ctx.ShouldBind(&cache)

	if err != nil {
		c.ResponseBadRequest(ctx)
		return
	}

	err = c.Service.Add(ctx, cache)
	c.HandleResponseNoResult(ctx, err, "添加缓存失败", "添加缓存成功")
}

// Remove
// @Tags 缓存管理
// @Summary 删除缓存
// @Produce  json
// @Param id query int required "缓存id"
// @Success 200 {object} api.Result "成功"
// @Router /cache [delete]
func (c *CacheController) Remove(ctx *gin.Context) {
	id, err := strconv.Atoi(ctx.Query("id"))
	if err != nil {
		c.ResponseBadRequest(ctx)
		return
	}
	err = c.Service.Remove(ctx, id)

	c.HandleResponseNoResult(ctx, err, "删除缓存失败", "删除缓存成功")
}

// Modify
// @Tags 缓存管理
// @Summary 修改缓存
// @Produce  json
// @Param cache body model.Cache required "缓存"
// @Success 200 {object} api.Result "成功"
// @Router /cache [put]
func (c *CacheController) Modify(ctx *gin.Context) {
	var cache model.Cache
	err := ctx.ShouldBind(&cache)

	if err != nil {
		c.ResponseBadRequest(ctx)
		return
	}
	err = c.Service.Modify(ctx, cache)
	c.HandleResponseNoResult(ctx, err, "修改缓存成功", "修改缓存失败")
}

// Find
// @Tags 缓存管理
// @Summary 缓存列表
// @Produce  json
// @Param pageNum query int false "页码"
// @Param pageSize query int false "每页数量"
// @Success 200 {object} api.Result "成功"
// @Router /cache/list [get]
func (c *CacheController) Find(ctx *gin.Context) {
	pageInfo, err := c.Service.Find(ctx)
	c.HandleResponseResult(ctx, err, "获取缓存列表失败", pageInfo)
}

// FindById
// @Tags 缓存管理
// @Summary 缓存by id
// @Produce  json
// @Param id query int false "缓存id"
// @Success 200 {object} api.Result "成功"
// @Router /cache [get]
func (c *CacheController) FindById(ctx *gin.Context) {
	id, err := strconv.Atoi(ctx.Query("id"))
	if err != nil {
		c.ResponseBadRequest(ctx)
		return
	}
	cache, err := c.Service.FindById(ctx, id)

	c.HandleResponseResult(ctx, err, "获取缓存失败", cache)
}

// AddCacheValue
// @Tags 缓存管理
// @Summary 添加缓存值
// @Produce  json
// @Param cache body model.CacheValue required "缓存值"
// @Success 200 {object} api.Result "成功"
// @Router /cache/value [post]
func (c *CacheController) AddCacheValue(ctx *gin.Context) {
	var cacheValue model.CacheValue
	err := ctx.ShouldBind(&cacheValue)
	if err != nil {
		c.ResponseBadRequest(ctx)
		return
	}

	err = c.Service.AddCacheValue(cacheValue)
	c.HandleResponseNoResult(ctx, err, "添加缓存值失败", "添加缓存值成功")
}

// RemoveCacheValue
// @Tags 缓存管理
// @Summary 删除缓存值
// @Produce  json
// @Param id query int required "缓存值id"
// @Success 200 {object} api.Result "成功"
// @Router /cache/value [delete]
func (c *CacheController) RemoveCacheValue(ctx *gin.Context) {
	id, err := strconv.Atoi(ctx.Query("id"))
	if err != nil {
		c.ResponseBadRequest(ctx)
		return
	}
	err = c.Service.RemoveCacheValue(id)
	c.HandleResponseNoResult(ctx, err, "删除缓存值失败", "删除缓存值成功")
}

// GetGlobalCacheValue
// @Tags 缓存管理
// @Summary 获取全局缓存值
// @Produce  json
// @Success 200 {object} api.Result "成功"
// @Router /globalCache [get]
func (c *CacheController) GetGlobalCacheValue(ctx *gin.Context) {
	globalValue, err := c.Service.GetGlobalCacheValue(ctx)
	c.HandleResponseResult(ctx, err, "获取全局缓存失败！", globalValue)
}

// RefreshGlobalCache
// @Tags 缓存管理
// @Summary 刷新全局缓存
// @Produce  json
// @Success 200 {object} api.Result "成功"
// @Router /refresh [get]
func (c *CacheController) RefreshGlobalCache(ctx *gin.Context) {
	initalize.RefreshCacheValue()
	c.Response(ctx, http.StatusOK, "刷新全局缓存成功", nil)
}
